import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torch import nn 
import copy
import numpy as np
from models.Update import DatasetSplit
from utils.save_result import save_result
from models.aggregation import Aggregation
from models.test import test_img
from models.test import test_img_avg
from models.branchnet import BranchNet
from torch.autograd import Variable

def KD(input_p,input_q,T = 1):
    kl_loss = nn.KLDivLoss(reduction="batchmean")
    p = F.log_softmax(input_p/T,dim=1)
    q = F.softmax(input_q/T,dim=1)
    result = kl_loss(p,q)
    return result

class LocalUpdate_FedTest(object):
    def __init__(self,args,dataset=None,idxs = None):
        self.args = args
        self.loss_func = nn.CrossEntropyLoss()
        self.ldr_train = DataLoader(DatasetSplit(dataset,idxs),self.args.local_bs,shuffle=True)
    def train(self,round,net):
        net.train()
        if self.args.optimizer == 'sgd':
            optimizer = torch.optim.SGD(net.parameters(), lr=self.args.lr*(self.args.lr_decay**round),
                                        momentum=self.args.momentum,weight_decay=self.args.weight_decay)
        elif self.args.optimizer == 'adam':
            optimizer = torch.optim.Adam(net.parameters(), lr=self.args.lr)
        epoch_loss = []
        for iter in range(self.args.local_ep):
            batch_loss = []
            for batch_idx,(images,labels) in enumerate(self.ldr_train):
                images,labels = images.to(self.args.device),labels.to(self.args.device)
                net.zero_grad()
                out_of_local = net(images)
                log_probs = out_of_local['output']
                loss = self.loss_func(log_probs,labels)
                loss.backward()
                optimizer.step()

                batch_loss.append(loss.item())
            epoch_loss.append(sum(batch_loss)/len(batch_loss))

        return net.state_dict()

class SeverUpdate_FedTest(object):
    def __init__(self,args,net_list,round ,dataset_global=None,dict_global = None):
        self.args = args
        self.loss_func = nn.CrossEntropyLoss()
        # self.net_s = net_s
        # self.net_list = net_list
        self.num_classes = args.num_classes
        self.net_state = []
        self.num_groups = args.num_groups
        self.models = []
        self.optimizer = []
        
        # 
        # 
        self.gdr_train = DataLoader(DatasetSplit(dataset_global,dict_global),batch_size=self.args.sever_bs,shuffle=True)
        self.alpha = args.alpha
        for i in range (self.num_groups):
            model = net_list[i].to(self.args.device)
            self.models.append(model)
            if self.args.optimizer == 'sgd':
                optimizer = torch.optim.SGD(model.parameters(), lr=self.args.lr*(self.args.lr_decay**round),
                                        momentum=self.args.momentum,weight_decay=self.args.weight_decay)
            elif self.args.optimizer == 'adam':
                optimizer = torch.optim.Adam(model.parameters(), lr=self.args.lr)
            self.optimizer.append(optimizer)
    def train(self):
        for i in range (self.num_groups):
            self.models[i].train()
        # net.train()
        # self.net_t.train()
        # self.net_t.eval()

        # if self.args.optimizer == 'sgd':
        #     optimizer = torch.optim.SGD(net.parameters(), lr=self.args.lr*(self.args.lr_decay**round),
        #                                 momentum=self.args.momentum,weight_decay=self.args.weight_decay)
        #     # optimizer = torch.optim.SGD([{'params':net.parameters()},{'params':self.branch_dict[64].parameters()},{'params':self.branch_dict[128].parameters()},
        #     #                             {'params':self.branch_dict[256].parameters()},{'params':self.branch_dict[512].parameters()}], 
        #     #                             lr=self.args.lr*(self.args.lr_decay**round),momentum=self.args.momentum,weight_decay=self.args.weight_decay)
        # elif self.args.optimizer == 'adam':
        #     optimizer = torch.optim.Adam(net.parameters(), lr=self.args.lr)
        epoch_loss = []
        for iter in range(self.args.sever_ep):
            batch_loss = []
            for batch_idx,(images,labels) in enumerate(self.gdr_train):
                images,labels = images.to(self.args.device),labels.to(self.args.device)
                images,labels = Variable(images),Variable(labels)
                # net.zero_grad()
                outputs = []
                for model in self.models:
                    outputs.append(model(images))
                for i in range(self.num_groups):
                    ce_loss = self.loss_func(outputs[i]['output'],labels)
                    kl_loss = 0
                    a = [s for s in range(self.num_groups)]
                    a.remove(i)
                    j = np.random.choice(a,1,replace=False)[0]
                    # kl_loss = KD(outputs[i]['representation5'],Variable(outputs[j]['representation5']),self.args.temp)
                    kl_loss = KD(outputs[i]['output'],Variable(outputs[j]['output']),self.args.temp)
                    loss = ce_loss + self.alpha * (kl_loss)
                    # print("kl_loss:",kl_loss)
                    # print("ce_loss:",ce_loss)

                    self.optimizer[i].zero_grad()
                    loss.backward()
                    self.optimizer[i].step()
                    
                    batch_loss.append(loss.item())
            epoch_loss.append(sum(batch_loss)/len(batch_loss))
        for model in self.models:
            self.net_state.append(copy.deepcopy(model.state_dict()))
        return self.net_state
        


        

def FedTest(args,net_list,dataset_train,dataset_test,dict_users,dict_global):
    num_net = len(net_list)
    # for i in range(num_net):
    #     net_list[i].train()
    if num_net != args.num_groups:
        exit("error num_groups")
    glob_net = copy.deepcopy(net_list[0])
    acc = []
    for iter in range(args.epochs):

        print('*'*80)
        print('Round {:3d}'.format(iter))

        m = max(int(args.frac*args.num_users),1)

        idxs_users = np.random.choice(range(args.num_users),m,replace=False)
        group_idxs_users = []
        for i in range(num_net):
            group_users = []
            start = i*int(m/num_net)
            end = (i+1)*int(m/num_net)
            for j in range(start,end):
                group_users.append(idxs_users[j])
            group_idxs_users.append(group_users)
        # # idxs_users = np.random.choice(range(args.num_users),m,replace=False)
        # list_users = [x for x in range(args.num_users)]
        # group_idxs_users = []
        # m1 = max(int(args.frac*args.num_users*args.group1_frac),1)
        # # group1_idxs_users = np.random.choice(range(0,int(args.num_users*args.group1_frac)),m1,replace=False)
        # group1_idxs_users = np.random.choice(list_users,m1,replace=False)
        # for x in group1_idxs_users:
        #     list_users.remove(x)
        # m2 = max(int(args.frac*args.num_users*args.group2_frac),1)
        # # group2_idxs_users = np.random.choice(range(int(args.num_users*args.group1_frac),int(args.num_users*(args.group1_frac+args.group2_frac))),m2,replace=False)
        # group2_idxs_users = np.random.choice(list_users,m2,replace=False)
        # # m3 = max(int(args.frac*args.num_users*args.group3_frac),1)
        # # group3_idxs_users = np.random.choice(range(int(args.num_users*(args.group1_frac+args.group2_frac)),args.num_users),m3,replace=False)
        # group_idxs_users.append(group1_idxs_users) 
        # group_idxs_users.append(group2_idxs_users)
        # # group_idxs_users.append(group3_idxs_users)
        w_globals = []
        w_list = []
        for id in range(args.num_groups):
            w_locals = []
            lens = []
            for idx in group_idxs_users[id]:
                local = LocalUpdate_FedTest(args=args,dataset=dataset_train,idxs=dict_users[idx])

                w_local = local.train(round=iter,net=copy.deepcopy(net_list[id]).to(args.device))
                w_locals.append(copy.deepcopy(w_local))
                lens.append(len(dict_users[idx]))
            # aggregation in group
            if len(lens) == 1:
                w_global = w_locals[0]
            else:
                w_global = Aggregation(w_locals,lens)
            # w_global = Aggregation(w_locals,lens)
            net_list[id].load_state_dict(w_global)
            w_globals.append(copy.deepcopy(w_global))
        agg_weights = []         
        for i in range(args.num_groups):
            agg_weights.append(1.0)
        w_global = Aggregation(w_globals,agg_weights)

        

        if iter < 50 or iter % 5 == 4:

            for i in range(args.num_groups):
                net_list[i].load_state_dict(copy.deepcopy(w_global))
        else:
            net_list_glob = []
            for id in range(args.num_groups):
                net_list_glob.append(copy.deepcopy(net_list[id]))    
                net_list_glob[id].load_state_dict(net_list[id].state_dict())    
            sever = SeverUpdate_FedTest(args=args,net_list = net_list_glob,round=iter,dataset_global=dataset_train,dict_global=dict_global)
            w_list = sever.train()
            for i in range(args.num_groups):
                net_list[i].load_state_dict(copy.deepcopy(w_list[i]))
        # for i in range(args.num_groups):
        #     net_list[i].load_state_dict(w_list[i])

        # agg_weights = []         
        # for i in range(args.num_groups):
        #     agg_weights.append(1.0)
        # w_global = Aggregation(w_globals,agg_weights)

        acc.append(test(net_list,dataset_test,args)) 
        # glob_net.load_state_dict(copy.deepcopy(w_global))      
        # acc.append(test_single(glob_net,dataset_test,args))

    save_result(acc,'test_acc',args)       


def test(net_list,dataset_test,args):

    acc_test,loss_test = test_img(net_list,dataset_test,args)

    print("Testing accuracy: {:.2f}, Testing loss: {:.2f}".format(acc_test,loss_test))


    return acc_test.item()

def test_single(net_glo,dataset_test,args):
    acc_test,loss_test = test_img_avg(net_glo,dataset_test,args)
    print("Testing accuracy: {:.2f}, Testing loss: {:.2f}".format(acc_test,loss_test))
    return acc_test.item()


        

